/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package eypweb_bsc;

import java.sql.*;

/**
 *
 * @author Kohalmi Istvan
 */

// egyszerusitett ertekpapir modell
public class Isin {
    // azonosito
    private int id;
    // nemzetkozi azonosito
    private String isin;
    // megnevezes
    private String name;
    // devizanem
    private Deviza deviza;
    // elszamolas alapja
    private String base;
    // alapcimlet
    private float denomination;
    // lejarat ideje
    private String expiry;
    // eszkozcsoport
    private String instrumentGroup;
    
    public Isin(){}

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the deviza
     */
    public Deviza getDeviza() {
        return deviza;
    }

    /**
     * @param deviza the deviza to set
     */
    public void setDeviza(Deviza deviza) {
        this.deviza = deviza;
    }

    /**
     * @return the base
     */
    public String getBase() {
        return base;
    }

    /**
     * @param base the base to set
     */
    public void setBase(String base) {
        this.base = base;
    }

    /**
     * @return the denomination
     */
    public float getDenomination() {
        return denomination;
    }

    /**
     * @param denomination the denomination to set
     */
    public void setDenomination(float denomination) {
        this.denomination = denomination;
    }

    /**
     * @return the expiry
     */
    public String getExpiry() {
        return expiry;
    }

    /**
     * @param expiry the expiry to set
     */
    public void setExpiry(String expiry) {
        this.expiry = expiry;
    }

    /**
     * @return the instrumentGroup
     */
    public String getInstrumentGroup() {
        return instrumentGroup;
    }

    /**
     * @param instrumentGroup the instrumentGroup to set
     */
    public void setInstrumentGroup(String instrumentGroup) {
        this.instrumentGroup = instrumentGroup;
    }
    
    // adott ISIN-hez aktualis ertekpapir objektumot rendel
    public static Isin getCurrentIsin(String isin)
    {
        Director director = Director.getInstance();
        AccessInfo ai = director.getAccessInfo();
        Isin isinObj = new Isin();
        try{
            Connection con = DriverManager.getConnection("jdbc:sqlserver://" + ai.getIPAddress() + ":" + ai.getPort() + ";databaseName=" + ai.getDBName(), ai.getUser(), ai.getPass());
            String isinQuery = "SELECT * FROM isin WHERE isin = ? AND ? BETWEEN hatalytol AND hatalyig";
            PreparedStatement isinStmt = con.prepareStatement(isinQuery);
            isinStmt.setNString(1, isin);
            isinStmt.setDate(2, new Date(new java.util.Date().getTime()));
            ResultSet isinRS = isinStmt.executeQuery();
            isinRS.next();
            isinObj.setId(isinRS.getInt(1));
            isinObj.setIsin(isin);
            isinObj.setName(isinRS.getNString(3));
            isinObj.setDeviza(Deviza.getCurrentDeviza(isinObj.getId()));
            isinObj.setExpiry(isinRS.getDate(5)==null?null:isinRS.getDate(5).toString());
            isinObj.setDenomination(isinRS.getFloat(4));
            
            String baseQuery = "SELECT megnev FROM befektetes_alap WHERE id = ?";
            PreparedStatement baseStmt = con.prepareStatement(baseQuery);
            baseStmt.setInt(1,isinRS.getInt(11));
            ResultSet baseRS = baseStmt.executeQuery();
            baseRS.next();
            isinObj.setBase(baseRS.getNString(1));
            
            String instrumentQuery = "SELECT eszkoz_csoport.megnev FROM eszkoz_csoport "
                    + "JOIN  isin_ecs ON isin_ecs.id_ecs=eszkoz_csoport.id "
                    + "JOIN isin ON isin_ecs.id_isin = isin.id "
                    + "WHERE isin.id = ? "
                    + "AND ? BETWEEN isin_ecs.hatalytol AND isin_ecs.hatalyig";
            PreparedStatement instrumentStmt = con.prepareStatement(instrumentQuery);
            instrumentStmt.setInt(1,isinObj.getId());
            instrumentStmt.setDate(2, new Date(new java.util.Date().getTime()));
            ResultSet instrumentRS = instrumentStmt.executeQuery();
            instrumentRS.next();
            isinObj.setInstrumentGroup(instrumentRS.getNString(1));
            
            con.close();
        }
        catch(SQLException sqle)
        {
            System.err.println(sqle.getMessage());
        }
        return isinObj;
    }

    /**
     * @return the isin
     */
    public String getIsin() {
        return isin;
    }

    /**
     * @param isin the isin to set
     */
    public void setIsin(String isin) {
        this.isin = isin;
    }
    
}
